﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;
using WindowsFormsApplication2.All_FORMS.Bvrtgel_forms;
using WindowsFormsApplication2.All_FORMS.Main_forms;
using System.Data.SqlClient;
using WindowsFormsApplication2.All_FORMS.Report_forms.Report;

namespace WindowsFormsApplication2
{   
    public partial class frmOrder : Form
    {
        SqlConnection conn;
        string naame;
        string _id= "101", BorluulaltId;
        string ss = "Идэвхитэй";
        string id;
        static int too;
        static string too1,OrdersId;
        private BindingSource bsource = new BindingSource();
        public enum clicktype
        {
            Add = 1,
            Edit = 2,

        }
        clicktype ConsructorType = new clicktype();
        public frmOrder(string name1, SqlConnection co)
        {
            InitializeComponent();
            ConsructorType = clicktype.Add;
            naame = name1;
            conn = co;
        }
        public frmOrder(Order _ord)
        {
            ConsructorType = clicktype.Edit;
            InitializeComponent();
            _id = _ord.OrderId;
            dateTimePicker1.Text = _ord.OrderAwsanOgnoo;
            dateTimePicker2.Text = _ord.OrderHvrgehOgnoo;
            textBox4.Text = _ord.OrderStatus;
            cmbProName.Text = _ord.ProductId;
            ProToo.Text = _ord.ProductToo;
            textBox5.Text = _ord.XTolologchId;
            textBox3.Text = _ord.CustomerId;
            int niit = 0;
            string nt;
            niit = Convert.ToInt32(ProToo.Text) * Convert.ToInt32(ProPrice.Text);
            nt = Convert.ToString(niit);
            nt = _ord.ProductId;

        }
        

        private void frmOrder_Load(object sender, EventArgs e)
        {   
            string aa = string.Format("select *from Orders where {0}=N'{1}' ", "OrderStatusId", ss);
            dataGridViewOrder.DataSource = DBTools.selecQryTmp(aa);
            FormatGrid();
           

        }


        public void blabla()
        {
            string aa = string.Format("select *from Product where {0}='{1}' ", "ProductId", cmbProName.Text);
            dataGridView1.DataSource = DBTools.selecQryTmp(aa);
            
        }

        private void FormatGrid1()
        {
            dataGridViewBorluulalt.AllowUserToOrderColumns = true;
            dataGridViewBorluulalt.AllowUserToAddRows = false;
            dataGridViewBorluulalt.AllowUserToDeleteRows = false;
            dataGridViewBorluulalt.ReadOnly = true;
            dataGridViewBorluulalt.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewBorluulalt.MultiSelect = true;

            FormatColumn1("BorluulaltId", "Борлуулалтын дугаар", 0);
            FormatColumn1("OrderId", "Захиалгын дугаар", 1);
            FormatColumn1("XTolologchId", "Худалдааны төлөөлөнчиин код", 2);
            FormatColumn1("TolborId", "Төлбөрийн код", 3);
            FormatColumn1("WorkerId", "Ажилтны код", 4);
        }
       
        public void FormatColumn(String colname, String caption, int index)
        {
            dataGridViewOrder.Columns[colname].HeaderText = caption;
            dataGridViewOrder.Columns[colname].DisplayIndex = index;
            dataGridViewOrder.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatColumn1(String colname, String caption, int index)
        {
            dataGridViewBorluulalt.Columns[colname].HeaderText = caption;
            dataGridViewBorluulalt.Columns[colname].DisplayIndex = index;
            dataGridViewBorluulalt.Columns[colname].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        public void FormatGrid()
        {
            dataGridViewOrder.AllowUserToOrderColumns = true;
            dataGridViewOrder.AllowUserToAddRows = false;
            dataGridViewOrder.AllowUserToDeleteRows = false;
            dataGridViewOrder.ReadOnly = true;
            dataGridViewOrder.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridViewOrder.MultiSelect = true;

            FormatColumn("OrderId", "Захиалгын дугаар", 0);
            FormatColumn("OrderAwsanOgnoo", "Захиалга авсан огноо", 1);
            FormatColumn("OrderHvrgehOgnoo", "Захиалга хүргэх огноо", 2);
            FormatColumn("OrderStatusId", "Захиалгын төлөв", 3);
            FormatColumn("ProductId", "Бүтээгдэхүүний код", 4);
            FormatColumn("ProductToo", "Тоо хэмжээ", 5);
            FormatColumn("XTolologchId", "Худалдааны төлөөлөгчийн код", 6);
            FormatColumn("CustomerId", "Харилцагчийн код", 7);
            FormatColumn("TolborId", "Төлбөрийн хэлбэр", 8);
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            int niit = 0;
            string nt;
            niit = Convert.ToInt32(ProToo.Text) * Convert.ToInt32(ProPrice.Text);
            nt = Convert.ToString(niit);
            if (ConsructorType == clicktype.Add)
            {
                Order.AddFunction(_id, dateTimePicker1.Text, dateTimePicker2.Text, textBox4.Text, cmbProName.Text, ProToo.Text, textBox5.Text,textBox3.Text, textBox6.Text);
                MessageBox.Show("Нэмэх үйлдэл");
                dataGridViewOrder.DataSource = DBTools.selecQryTmp("Select *from Orders");
            }
            else
            {

                Order.EditFunction(_id, dateTimePicker1.Text, dateTimePicker2.Text, textBox4.Text, cmbProName.Text, ProToo.Text, textBox5.Text,textBox3.Text, textBox6.Text);
                MessageBox.Show("Засах үйлдэл");
                dataGridViewOrder.DataSource = DBTools.selecQryTmp("Select *from Orders");

            }
         
            
            ProToo.Text = "";
            nt = "";
            dataGridViewOrder.DataSource = DBTools.selecQryTmp("select *from Orders");
           // FormatGrid();

        }

        

        private void cmbProName_SelectedIndexChanged(object sender, EventArgs e)
        {
            blabla();
            int i = 0;
            
            i = dataGridView1.CurrentRow.Index;
            DataRowView drv = dataGridView1.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string proId = drv[0].ToString();
                string proName = drv[1].ToString();
                string proprice = drv[4].ToString();

                textBox1.Text = proName;
                ProPrice.Text = proprice;
            }

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("Та цуцлана гэдэгтээ итгэлтэй байна уу?", "Анхаар", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
            {
                try
                {
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message, "Алдаа", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }


        private void button4_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            SqlCommand comm = new SqlCommand("Select * from  Borluulalt where OrderId = '" + textBox2.Text + "'", conn);
            SqlDataReader reader = comm.ExecuteReader();
            if (reader.HasRows == true)
            {
                MessageBox.Show("Тус захиалга борлуулалтанд орсон байна ! ! !");
            }

            else
            {
                timer1.Enabled = true;
                Borluulalt bor = new Borluulalt();
                bor.WorkerId = naame;
                bor.BorluulaltId = _id;
                bor.OrderId = textBox2.Text;
                bor.XTolologchId = textBox5.Text;
                bor.TolborId = textBox6.Text;
                Borluulalt.AddFunction(_id, textBox2.Text, textBox5.Text, textBox6.Text, naame);
                if (dataGridViewOrder.CurrentRow != null)
                {
                   // string aa = string.Format("insert into Borluulalt values ('{0}','{1}','{2}','{3}','{4}')", textBox2.Text,textBox5.Text, textBox6.Text, "102");
                   // dataGridView1.DataSource = DBTools.selecQryTmp(aa);
                    string aa1 = string.Format("select *from Borluulalt where OrderId = '{0}'", textBox2.Text);
                    dataGridViewBorluulalt.DataSource = DBTools.selecQryTmp(aa1);
                    FormatGrid1();
                   
                    Init();
                    //  string aa = string.Format("create view Borluulalt_view as select Borluulalt.BorluulaltId as 'Борлуулалт дугаар', Borluulalt.OrderId as 'Захиалга дугаар', Borluulalt.WorkerId as 'Ажилтны нэр', Borluulalt.XTolologchId as 'Худалдааны төлөөлөгч', Bor_Product.ProductId as 'Бүтээгдэхүүн', Borluulalt.TolborId as 'Төлбөр', Orders_Product.UnitPrice as 'Нийт үнэ', Orders_Product.UnitWeight as 'Нийт тоо' from  Borluulalt,Orders_Product, Bor_Product where Borluulalt.OrderId = Orders_Product.OrderId and Borluulalt.BorluulaltId = Bor_Product.BorluulaltId");
                    //  dataGridViewBor.DataSource = DBTools.selecQryTmp(aa);

                    string aa2 = string.Format("select *from Borluulalt_view where {0}='{1}' and {2}=N'{3}'", "Borluulalt_view.[Борлуулалт дугаар]", id, "Borluulalt_view.[Захиалгын төлөв]", "Идэвхитэй");
                    dataGridViewBor.DataSource = DBTools.selecQryTmp(aa2);
                    tabControl1.SelectedTab = tabPage3;
                }

                tabControl1.SelectedTab = tabPage3;
            }
            reader.Close();


            

        }

        private void Init()
        {
            int i = 0;

            i = dataGridViewBorluulalt.CurrentRow.Index;
            DataRowView drv = dataGridViewBorluulalt.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string proId = drv[0].ToString();

                id = proId;

            }
            string aa = string.Format("insert into Bor_Product values ('{0}','{1}',N'{2}')", id, cmbProName.Text, textBox6.Text);
            dataGridView1.DataSource = DBTools.selecQryTmp(aa);

            string aa1 = string.Format("insert into Orders_Product values ('{0}','{1}','{2}','{3}')", textBox11.Text, textBox12.Text, textBox2.Text, cmbProName.Text);
            dataGridView1.DataSource = DBTools.selecQryTmp(aa1);

        }

        private void button5_Click_1(object sender, EventArgs e)
        {
           
        }

        private void dataGridViewOrder_MouseClick(object sender, MouseEventArgs e)
        {
             int i = 0;

             i = dataGridViewOrder.CurrentRow.Index;
             DataRowView drv = dataGridViewOrder.CurrentRow.DataBoundItem as DataRowView;

             if (drv != null)
             {
                 string orderid = drv[0].ToString();
                 string orderhvrgehognoo = drv[2].ToString();
                 string orderstatus = drv[3].ToString();
                 string productid = drv[4].ToString();
                 string producttoo = drv[5].ToString();
                 string xttolologchid = drv[6].ToString();
                 string cusid = drv[7].ToString();
                 string tolbor = drv[8].ToString();
                 textBox2.Text = orderid;
                 ProToo.Text = producttoo;
                 dateTimePicker2.Text = orderhvrgehognoo;
                 cmbProName.Text = productid;
                 ProToo.Text = producttoo;
                 textBox4.Text = orderstatus;
                 textBox5.Text = xttolologchid;
                 textBox3.Text = cusid;
                 textBox6.Text = tolbor;
                 textBox12.Text = Convert.ToString(Convert.ToInt32(ProPrice.Text) * Convert.ToInt32(ProToo.Text));
                 textBox11.Text = ProToo.Text;
             }
        }

        private void cmbProName_TextChanged(object sender, EventArgs e)
        {
            blabla();
            int i = 0;

            i = dataGridView1.CurrentRow.Index;
            DataRowView drv = dataGridView1.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string proId = drv[0].ToString();
                string proName = drv[1].ToString();
                string proprice = drv[4].ToString();

                textBox1.Text = proName;
                ProPrice.Text = proprice;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Height <= 371) timer1.Enabled = false;
            else this.Height -= 10;
           
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            if (this.Height >= 498) timer2.Enabled = false;
            else this.Height += 10;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            
            
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string aa = string.Format("select *from Orders where {0}=N'{1}' ", "OrderStatusId", ss);
            dataGridViewOrder.DataSource = DBTools.selecQryTmp(aa);
            FormatGrid();
            timer2.Enabled = true;
            tabControl1.SelectedTab = tabPage1;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            
            string aa2 = string.Format("select *from Product where ProductId = '{0}'", cmbProName.Text);
            dataGridView1.DataSource = DBTools.selecQryTmp(aa2);
            haha();
            too = Convert.ToInt32(too1)- Convert.ToInt32(ProToo.Text);

            string aa = string.Format("update Product set {0}='{2}' where {1}='{3}'","ProductToo",  "ProductId", too, cmbProName.Text);
            dataGridView2.DataSource = DBTools.selecQryTmp(aa);
            string aa3 = string.Format("update Orders set OrderStatusId = N'Ok' where {0}='{1}'", "OrderId", textBox2.Text);
            dataGridView1.DataSource = DBTools.selecQryTmp(aa3);
            int i = 0;

            i = dataGridViewBor.CurrentRow.Index;
            DataRowView drv = dataGridViewBorluulalt.CurrentRow.DataBoundItem as DataRowView;

            if (drv != null)
            {
                string proId = drv[0].ToString();

                BorluulaltId = proId;

            }
            frmTailanBarimt frm = new frmTailanBarimt(BorluulaltId);
            frm.Show();
        }

        private void haha()
        {int i = 0;

             i = dataGridView1.CurrentRow.Index;
             DataRowView drv = dataGridView1.CurrentRow.DataBoundItem as DataRowView;

             if (drv != null)
             {

                 string proid = drv[0].ToString();
                  too1 = drv[5].ToString();
               
             }
        }

        private void tabControl1_DoubleClick(object sender, EventArgs e)
        {
            string aa1 = string.Format("select *from Borluulalt");
            dataGridViewBorluulalt.DataSource = DBTools.selecQryTmp(aa1);
            FormatGrid1();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
             string aa2 = string.Format("select *from Borluulalt_view");
            dataGridViewBor.DataSource = DBTools.selecQryTmp(aa2);
        }

        private void button9_Click(object sender, EventArgs e)
        {

            
                int i = 0;
                i = dataGridViewBor.CurrentRow.Index;
                DataRowView drv = dataGridViewBor.CurrentRow.DataBoundItem as DataRowView;

                if (drv != null)
                {
                    string proId = drv[0].ToString();
                    string orderid = drv[1].ToString();
                        id = proId;
                         OrdersId = orderid;
                }


                string aa11 = string.Format("delete from Bor_Product where BorluulaltId = '{0}'", id);
                dataGridView1.DataSource = DBTools.selecQryTmp(aa11);
                string aa1 = string.Format("delete from Borluulalt where BorluulaltId = '{0}'", id);
                dataGridView1.DataSource = DBTools.selecQryTmp(aa1);
                string aa111 = string.Format("delete from Orders_Product where OrderId = '{0}'", OrdersId);
                dataGridView1.DataSource = DBTools.selecQryTmp(aa111);

                string aa2 = string.Format("select *from Borluulalt where OrderId = '{0}'", OrdersId);
                dataGridViewBorluulalt.DataSource = DBTools.selecQryTmp(aa2);
                string aa3 = string.Format("select *from Borluulalt_view");
                dataGridViewBor.DataSource = DBTools.selecQryTmp(aa3);
            
        }
       
    }

}
